public class RomanToInt {public int romanToInt(String s) {
    int n=s.length();
    int sum=0;
    for(int i=0;i<n;i++){
        char ch=s.charAt(i);
        if(ch=='I'){
            if(i==n-1){
                sum+=1;
            }else{
                i++;
                ch=s.charAt(i);
                if(ch=='V'){
                    sum+=4;
                }else if(ch=='X'){
                    sum+=9;
                }else{
                    i--;
                    sum+=1;
                }
            }
        }
        else if(ch=='V'){
            sum+=5;
        }else if(ch=='L'){
            sum+=50;
        }
        else if(ch=='D'){
            sum+=500;
        }else if(ch=='M'){
            sum+=1000;
        }else if(ch=='X'){
            if(i==n-1){
                sum+=10;
            }else{
                i++;
                ch=s.charAt(i);
                if(ch=='L'){
                    sum+=40;
                }else if(ch=='C'){
                    sum+=90;
                }else{
                    i--;
                    sum+=10;
                }
            }
        }else if(ch=='C'){
            if(i==n-1){
                sum+=100;
            }else{
                i++;
                ch=s.charAt(i);
                if(ch=='D'){
                    sum+=400;
                }else if(ch=='M'){
                    sum+=900;
                }else{
                    i--;
                    sum+=100;
                }
            }
        }
    }
    return sum;
}
}
